import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_base/themes/app_theme.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/adapters.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'const/app_policies.dart';
import 'core/core_kit_localizations.dart';
import 'core/toast.dart';
import 'generated/l10n.dart';
import 'route/app_pages.dart';
import 'service/app_service.dart';
import 'service/auth_service.dart';
import 'themes/multiple_themes_mode/app_theme_default.dart';
import 'utils/encryp_utils.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await initApp();

  runApp(const MyApp());
}

//初始化App调用
Future<void> initApp() async {
  // //开启代理抓包
  // HttpProxyOverride httpProxyOverride =
  //     await HttpProxyOverride.createHttpProxy();
  // if (Env.appEnv != EnvName.release) {
  //   if (httpProxyOverride.host != null && httpProxyOverride.port != null) {
  //     HttpOverrides.global = httpProxyOverride;
  //   }
  // }

  // 初始化数据库，必须放在最前面
  await Hive.initFlutter();

  //图片缓存
  AppPolicies.imageCacheBox = await Hive.openBox('imageCache');
  //视频缓存
  AppPolicies.videoCacheBox = await Hive.openBox('videoCacheBox');

  //初始化加密信息
  await EncryptUtils.initAes();

  //这个是服务App的包括主题 国际化都在这里初始化
  Get.put(AppService());

  //強制app豎屏
  await SystemChrome.setPreferredOrientations(<DeviceOrientation>[
    DeviceOrientation.portraitUp,
    // DeviceOrientation.portraitDown
  ]);

  if (!kIsWeb && Platform.isAndroid) {
    const systemUiOverlayStyle = SystemUiOverlayStyle(
      statusBarColor: Colors.transparent, //设置为透明
      statusBarIconBrightness: Brightness.light,
      systemNavigationBarColor: Colors.transparent,
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    await SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(430, 932),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child) {
        return GetMaterialApp(
          title: 'FlutterBase',
          builder: (context, child) {
            return CoreKitToastInit()(
              context,
              Material(
                child: CupertinoTheme(
                  data: AppThemeDefault.cupertinoTheme(
                      brightness: isDarkMode(context)
                          ? Brightness.dark
                          : Brightness.light),
                  child: child ?? Container(),
                ),
              ),
            );
          },
          theme: AppThemeDefault.lightTheme,
          darkTheme: AppThemeDefault.darkTheme,
          themeMode: ThemeMode.values[AppService.to.themeMode.value],
          localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
            GlobalCupertinoLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            RefreshLocalizations.delegate,
            CoreKitLocalizations.delegate,
            AppIntl.delegate,
          ],
          locale: AppService.to.locale,
          initialRoute: AppPages.INITIAL,
          initialBinding: BindingsBuilder(
            () {
              Get.put(AuthService());
            },
          ),
          supportedLocales: AppIntl.delegate.supportedLocales,
          getPages: AppPages.routes,
          navigatorObservers: <NavigatorObserver>[
            CoreKitToastNavigatorObserver()
          ],
          // logWriterCallback: localLogWriter,
          // routingCallback: (Routing? routing) {
          //   // CoreKitLogger().e(routing?.current);
          // },
        );
      },
    );
  }

// void localLogWriter(String text, {bool isError = false}) {
//   // 在这里把信息传递给你最喜欢的日志包。
//   // 请注意，即使enableLog: false，日志信息也会在这个回调中被推送。
//   // 如果你想的话，可以通过GetConfig.isLogEnable来检查这个标志。
// }
}
